import req from "../req.js"
import cfg from "../config.js"
import report_page_type from "./report_page_type.js"

let page_type = {
    open: function (page_info) {
        let page_html_id = page_info.page_html_id;
        let property = page_info.property;
        let ext_js = page_info.ext_js;
        let page_id = page_info.page_id;
        let rights = page_info.rights;
        let params = page_info.params;
        $("#" + page_html_id).addClass("report-quick-builder-page row");
        $("#" + page_html_id).html("\
        <style>.report-quick-builder-page .amis-scope .antd-Page-body{padding:0;}</style>\
        <div class='col-md-8 col-sm-12 col-xs-12' style='padding:0.8rem;'>\
            <div class='view-part' style='border:dashed 1px #c3c3c3;min-height:100px;padding:0.2rem;overflow-x:scroll;'>\
                <table class='report-canvas' border=1 cellspacing=0 style='width:100%;border:#cfcfcf;'>\
                </table>\
            </div>\
        </div>\
        <div class='col-md-4 col-sm-12 col-xs-12' style='padding:0.8rem;'>\
            <div class='config-part'></div>\
        </div>");

        let amisJSON = {
            "type": "page",
            "name": page_html_id + "_config",
            "body": [
                {
                    "type": "form",
                    "name": "config_form",
                    "api": cfg.api_server + "reportbuilder/build",
                    "title": "报表配置",
                    "resetAfterSubmit": true,
                    "redirect": "index.html", //?pid=${page_id}
                    "persistData": "curd-quick-builder-page",
                    "clearPersistDataAfterSubmit": true,
                    "multiple": true,
                    "className": "m-t",
                    "actions": [
                        {
                            "type": "reset",
                            "label": "重置"
                        },
                        {
                            "type": "submit",
                            "level": "primary",
                            "label": "生成报表"
                        }
                    ],
                    "controls": [
                        {
                            "type": "service",
                            "name": "configer",
                            "data": {
                                "api_server": cfg.api_server,
                                "report_server": cfg.report_server
                            },
                            "schemaApi": {
                                "method": "post",
                                "url": cfg.api_server + "configer/getbyname",
                                "requestAdaptor": function (api) {
                                    api.data.name = "report_builder";
                                },
                                "adaptor": function (payload, response) {
                                    return payload;
                                }
                            }
                        }
                    ]
                }
            ]
        }
        let amis = amisRequire('amis/embed');
        let config_amisScoped = amis.embed(
            '#' + page_html_id + " .config-part", amisJSON, {},
            {
                theme: 'antd'
            }
        );

        let pre_config = null;
        let refresh = function () {
            if ($('#' + page_html_id + " .view-part").length == 0) return;
            let canvas = $('#' + page_html_id + " .view-part .report-canvas");
            let data = config_amisScoped.getComponentByName(page_html_id + "_config.config_form").props.data;
            if (JSON.stringify(data) == JSON.stringify(pre_config)) return;
            else pre_config = data;
            report_page_type.render(page_html_id,canvas,data);
        }
        refresh();
        //定时同步刷新预览
        let interval = window.setInterval(refresh, 3000);
    }
}

export default page_type;